<?php
/**
 * 团购管理
 * Created by Sublime Text 3.
 * @authors Rejeo (rejeo@qq.com)
 * @date    2018-05-01 16:31:32
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\common\model\Deal as DealModel;

class Deal extends Controller
{
    private $link;

    public function _initialize()
    {
        $this->link = new DealModel();
    }

    public function index(Request $request)
    {
        $sdata = [];
        $cityArr = [];
        $categoryArr = [];
        $data = $request->get();
        $start_time = empty($data['start_time'])?'':strtotime($data['start_time']);
        $end_time = empty($data['end_time'])?'':strtotime($data['end_time']);
        if (!empty($start_time) && !empty($end_time) && $end_time>$start_time) {
            $sdata['create_time'] = [
                ['gt', $start_time],
                ['lt', $end_time],
            ];
        }
        if (!empty($data['category_id'])) {
            $sdata['category_id'] = $data['category_id'];
        }
        if (!empty($data['city_id'])) {
            $sdata['city_id'] = $data['city_id'];
        }
        if (!empty($data['name'])) {
            $sdata['name'] = ['like', '%'.$data['name'].'%'];
        }

        // 获取一级城市
        $citys = model('city')->getNormalCitys();
        foreach ($citys as $va) {
            $cityArr[$va->id] = $va->name;
        }
        // 获取一级栏目
        $categorys = model('category')->getCategorysToParentId();
        foreach ($categorys as $v) {
            $categoryArr[$v->id] = $v->name;
        }
        // 搜索数据
        $res = $this->link->getNormalDeals($sdata);
        $page = $res->render();
        return $this->fetch('',[
            'citys'       => $citys,
            'categorys'   => $categorys,
            'res'         => $res,
            'name'        => empty($data['name'])?'':$data['name'],// 前端用来处理选中状态
            'category_id' => empty($data['category_id'])?'':$data['category_id'],
            'city_id'     => empty($data['city_id'])?'':$data['city_id'],
            'start_time'  => empty($data['start_time'])?'':$data['start_time'],
            'end_time'    => empty($data['end_time'])?'':$data['end_time'],
            'cityArr'     => $cityArr,
            'categoryArr' => $categoryArr,
            'page'        => $page,
        ]);
    }
}
